package com.jetpack.fixpatch

import com.android.build.gradle.api.ApkVariant
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import com.jetpack.fixpatch.extension.*
import com.jetpack.fixpatch.task.*

class FixPatchPlugin implements Plugin<Project> {
    Project project
    @Override
    void apply(Project target) {
        project = target
        target.extensions.create("fixpatch",FixPatchExtension)
        target.fixpatch.extensions.create("buildConfig",FixPatchBuildConfigExtension)
        target.fixpatch.extensions.create("dex",FixPatchDexExtension)
        target.fixpatch.extensions.create("lib",FixPatchLibExtension)
        target.fixpatch.extensions.create("resource",FixPatchResourceExtension)

        if (!target.plugins.hasPlugin('com.android.application')){
            throw new GradleException('generateFixPatchApk: Android Application plugin required')
        }

        def android = target.extensions.android

        target.afterEvaluate {
            def fixpatch = target.fixpatch
            if (!fixpatch.fixpatchEnable){
                target.logger.error("fixpatch tasks are disabled.")
                return
            }

            android.applicationVariants.all{ ApkVariant variant ->
                def variantName = variant.name
                def variantCapitalize = variantName.capitalize()

                FixPatchSchemaTask fixPatchSchemaTask = target.tasks.create("FixPatch$variantCapitalize", FixPatchSchemaTask)
                fixPatchSchemaTask.signConfig = variant.signingConfig

                //获取APP的Java源代码目录
                File file = variant.sourceSets.get(0).javaDirectories.getAt(0)  //  F:\hdfs\jetpackframework\test\src\main\java
                List<File> patchFiles = getPatchFile(file,android.defaultConfig.versionCode)
                patchFiles.each {
                    target.logger.error(it.getAbsolutePath())
                }

                //执行dex class差分包生成的Task
                FixPatchDexTask fixPatchDexTask = target.tasks.create("FixPatchDex$variantCapitalize",FixPatchDexTask,target.buildDir.getAbsolutePath()+"/intermediates/javac/$variantName/classes",patchFiles)
                fixPatchDexTask.dependsOn Compatibilities.getAssembleTask(target,variant)


            }
        }
    }
    List<File> getPatchFile(file,versionCode){
        List<File> patchs = new ArrayList<>()
        File[] files = file.listFiles()
        files.each {
            if (it.isDirectory()){
                List<File> patchFile = getPatchFile(it,versionCode)
                patchs.addAll(patchFile)
            }else {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(it)))
                String line = null
                boolean isFlag = false
                while ((line = br.readLine()) != null){
                    if (line.contains('com.gwm.annotation.fixpatch')){
                        isFlag = true
                    }
                    if (line.contains("@FixPatchClass(") && isFlag){
                        line = line.replaceAll(" ","").replaceAll("\\)","").split("=")[1]
                        if (Integer.parseInt(line) > versionCode){
                            patchs.add(it)
                        }
                    }
                }
                br.close()
            }
        }
        return patchs
    }
}